package com.yeyks.controller.other;

import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.param.CashOutDownloadParam;
import com.yeyks.common.param.CashOutQueryParam;
import com.yeyks.common.result.CashOutDownloadResult;
import com.yeyks.common.utils.IpUtil;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.consumer.CashOutOrderService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "提现接口信息", description = "提现相关信息接口")
@RestController
@RequestMapping("/ajax/cashout")
public class CashOutController {


    @Autowired
    private CashOutOrderService cashOutService;


    @ApiOperation("分页查询提现接口")
    @PostMapping("/list")
    public ResponseEntity<AjaxResponseBody> queryList(@Valid CashOutQueryParam cashOutQueryParam,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        return cashOutService.queryList(cashOutQueryParam, pageNum, pageSize);
    }

    @ApiOperation("确认打款")
    @PostMapping("/confirm")
    public ResponseEntity<AjaxResponseBody> confirmMoney(@RequestParam(value = "token", required = true) String token,
                                                         @RequestParam(value = "id", required = true) Long id,
                                                         HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        return cashOutService.confirmMoney(token, id, "47.112.120.109");
    }

    @ApiOperation("取消打款")
    @PostMapping("/refund")
    public ResponseEntity<AjaxResponseBody> refundMoney(@RequestParam(value = "token", required = true) String token,
                                                        @RequestParam(value = "id", required = true) Long id,
                                                        @RequestParam(value = "remark", required = false) String remark) {

        return cashOutService.refundMoney(token, id, remark);
    }

    /**
     * 导出订单
     *
     * @param response
     * @return
     */
    @NeedToken
    @ApiOperation(value = "导出订单", notes = "导出订单")
    @PostMapping(value = "/downOrder")
    public void downOrder(@Validated CashOutDownloadParam param, HttpServletResponse response) {
        List<CashOutDownloadResult> orderDownloadResults = cashOutService.selectDownloadOrder(AccountUtils.getLoginUser(), param);
        ExcelUtil.disposeWriteExcelWithSheets(response, orderDownloadResults, new CashOutDownloadResult());
    }


}
